using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using WindowsLive.Writer.Api;
using Wilco.SyntaxHighlighting;
using WindowsLive.Writer.ApplicationFramework.Skinning;
using System.Drawing.Text;
using WindowsLive.Writer.ApplicationFramework;

namespace CodeSyntaxHighLighter
{
	public partial class ContentEditor : SmartContentEditor, IDisposable	
	{
		public Bitmap WaterMarkImage
		{
			get { return Properties.Resources.import; }
		}


		WindowsLive.Writer.Api.ISmartContentEditorSite _editorSite;
		public ContentEditor(WindowsLive.Writer.Api.ISmartContentEditorSite editorSite)
		{
			InitializeComponent();
			_colorPicker.Text = "Background Color";
			//_codeBlockSize.DataBindings.Add("Visible", _fixedSizeCheck, "Checked");
			_widthText.DataBindings.Add("Enabled", _fixedWidthCheck, "Checked");
			_heighText.DataBindings.Add("Enabled", _fixedHeighCheck, "Checked");
			//_backgroundColor.DataBindings.Add("Enabled", _backgroundCheck, "Checked");

			_editorSite = editorSite;
			_editorSite.ContentResized += _editorSite_ContentResized;

			HighLighterUtils.FillInHighLighters(_imageComboBox);
		}

		void _editorSite_ContentResized(object sender, EventArgs e)
		{
			FillInSize();
		}

		private void _widthText_Validating(object sender, CancelEventArgs e)
		{
			int defaultValue;
			string key;
			if (sender == _widthText)
			{
				key = "Width";
				defaultValue = AdvancedPlugin.DefaultWidth;
			}
			else if (sender == _heighText)
			{
				key = "Height";
				defaultValue = AdvancedPlugin.DefaultHeight;
			}
			else return;

			bool result = true;
			TextBox box = (TextBox)sender;
			string value = box.Text.Trim();

			int intValue = -1;
			if (Int32.TryParse(value, out intValue))
			{
				if (intValue < 0) result = false;
			}
			else
				result = false;

			if (result)
			{
				SelectedContent.Properties.SetString(key, value);
				OnContentEdited();
			}
			else
				box.Text = SelectedContent.Properties.GetString(key, defaultValue.ToString());
		}

		private void _imageComboBox_SelectionChangeCommitted(object sender, EventArgs e)
		{
			ImageComboItem item = (ImageComboItem)_imageComboBox.SelectedItem;
			string selectedLanguage = item.Text;
			_languageLabel.Text = selectedLanguage;
			SelectedContent.Properties.SetString("Language", selectedLanguage);
			OnContentEdited();
		}

		private void settingsCheckBoxes_CheckedChanged(object sender, EventArgs e)
		{
			CheckBox box = (CheckBox)sender;
			string key = box.Tag.ToString();
			SelectedContent.Properties.SetBoolean(key, box.Checked);
			OnContentEdited();
		}

		protected override void OnSelectedContentChanged()
		{
			base.OnSelectedContentChanged();

			_fixedSizeCheck.Checked = SelectedContent.Properties.GetBoolean(_fixedSizeCheck.Tag.ToString(), false);
			_fixedWidthCheck.Checked = SelectedContent.Properties.GetBoolean(_fixedWidthCheck.Tag.ToString(), false);
			_fixedHeighCheck.Checked = SelectedContent.Properties.GetBoolean(_fixedHeighCheck.Tag.ToString(), false);
			//_backgroundCheck.Checked = SelectedContent.Properties.GetBoolean(_backgroundCheck.Tag.ToString(), false);
			_colorPicker.Color = ColorTranslator.FromHtml(SelectedContent.Properties.GetString("BackgroundColor", "#F7F7FF"));
			_showBorder.Checked = SelectedContent.Properties.GetBoolean(_showBorder.Tag.ToString(), false);

			FillInSize();

			string language = SelectedContent.Properties.GetString("Language", "C#");
			ImageComboItem lngItem;
			foreach (Object item in _imageComboBox.Items)
			{
				lngItem = (ImageComboItem)item;
				if (lngItem.Text == language)
				{
					_imageComboBox.SelectedItem = lngItem;
					break;
				}
			}
		}

		private void FillInSize()
		{
			_widthText.Text = SelectedContent.Properties.GetString("Width", AdvancedPlugin.DefaultWidth.ToString());
			_heighText.Text = SelectedContent.Properties.GetString("Height", AdvancedPlugin.DefaultHeight.ToString());
		}

		#region IDisposable Members

		void IDisposable.Dispose()
		{
			_editorSite.ContentResized -= _editorSite_ContentResized;
		}

		#endregion


		private void _editButton_Click(object sender, EventArgs e)
		{
			string language = ((ImageComboItem)_imageComboBox.SelectedItem).Text;
			string code = SelectedContent.Properties.GetString("Code", String.Empty);

			OptionsForm form = new OptionsForm();
			form.Language = language;
			form.CodeText = code;

			if (DialogResult.OK == form.ShowDialog())
			{
				SelectedContent.Properties.SetString("Code", form.CodeText);
				SelectedContent.Properties.SetString("Language", form.Language);
				OnContentEdited();
			}
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			if (_codeBlockLabel.ForeColor != ColorizedResources.Instance.SidebarHeaderTextColor)
			{
				_codeBlockLabel.ForeColor = ColorizedResources.Instance.SidebarHeaderTextColor;
				_languageLabel.ForeColor = ColorizedResources.Instance.SidebarHeaderTextColor;
			}

			base.OnPaint(e);
		}

		private void _imageComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{

		}

		private void _colorPicker_ColorSelected(object sender, ColorSelectedEventArgs args)
		{
			string htmlColor =  ColorTranslator.ToHtml(args.SelectedColor);
			SelectedContent.Properties.SetString("BackgroundColor", htmlColor);
			OnContentEdited();
		}
	}
}
